// app.js
App({
  onLaunch() {
    // 获取胶囊信息
    let menuButtonObject = wx.getMenuButtonBoundingClientRect()

    // 获取设备信息
    const windowInfo = wx.getWindowInfo()
    // 整个导航栏的高度
    let navHeight = windowInfo.statusBarHeight + menuButtonObject.height + (menuButtonObject.top - windowInfo.statusBarHeight) * 2 + 4
    // 导航栏的高度
    let nav = navHeight - windowInfo.statusBarHeight
    //web区高度
    let webViewHeight = windowInfo.windowHeight - navHeight

    let deviceWidth = windowInfo.windowWidth //获取设备屏幕宽度
    let searchHeight = Math.floor((deviceWidth / 750) * Number(70))

    // 挂载到全区变量 globalData 上
    this.globalData.navHeight = navHeight
    this.globalData.nav = nav
    this.globalData.webView = webViewHeight
    this.globalData.searchHeight = searchHeight
    // 胶囊与左边的距离
    this.globalData.menuLeft = menuButtonObject.left
    // 胶囊的高度
    this.globalData.menuHeight = menuButtonObject.height
    // 胶囊距离顶部的高度
    this.globalData.menuBot = menuButtonObject.top - windowInfo.statusBarHeight
    this.globalData.menuBottom = menuButtonObject.bottom
    // 整个设备的宽度
    this.globalData.windowWidth = windowInfo.windowWidth
    this.globalData.windowHeight = windowInfo.windowHeight
    // 胶囊按钮右边坐标 - 胶囊按钮宽度 = 按钮左边可使用宽度
    this.globalData.menuWidth = menuButtonObject.right - menuButtonObject.width - (deviceWidth - menuButtonObject.right)
    this.globalData.menuTop = menuButtonObject.top

    this.globalData.StatusBar = windowInfo.statusBarHeight
    let custom = wx.getMenuButtonBoundingClientRect()
    this.globalData.Custom = custom
    this.globalData.CustomBar = custom.bottom + custom.top - windowInfo.statusBarHeight

    // 展示本地存储能力
    const logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    // 将是否iphoneX 信息缓存到全局公众数据中
    this.globalData.isIphoneX = this.isIphoneX()
  },
  onShareTimeline() {
    return {
      title: '晨熙-全类目冻品商城', // 朋友圈显示的标题（非必须）
      path: '/pages/home/index' // 指定分享后跳转的页面路径（非必须）
    }
  },
  // iphoneX兼容 有刘海屏
  isIphoneX() {
    let mobile = wx.getWindowInfo()
    this.globalData.safeAreaTop = mobile.safeArea.top
    if (mobile.safeArea.top > 20) {
      return true
    } else {
      return false
    }
  },
  // 常用正则表达式集
  regExps: {
    email: /^[0-9a-zA-Z_]+@[0-9a-zA-Z_]+[\.]{1}[0-9a-zA-Z]+[\.]?[0-9a-zA-Z]+$/, //邮箱
    mobile: /^(?:1\d{2})-?\d{5}(\d{3}|\*{3})$/, //手机号码
    qq: /^[1-9][0-9]{4,9}$/, //QQ
    befitName: /^[a-z0-9A-Z\u4e00-\u9fa5]+$/, //合适的用户名，中文,字母,数字
    befitPwd: /^[a-z0-9A-Z_]+$/, //合适的用户名，字母,数字,下划线
    allNumber: /^[0-9]+.?[0-9]$/ //全部为数字
  },
  // 获取上一页路由
  getPage(index) {
    const pages = getCurrentPages()
    const prevPage = pages[pages.length - index]
    return prevPage
  },
  globalData: {
    userInfo: null,
    isIphoneX: false,
    addrId: 0,
    safeAreaTop: 20,
    img_path: 'https://lq.elongtian.com',
    cateid: false,
    position: null
  }
})
